<?php

/*
 * litchicms
 * Copyright (C) 2010-5-25 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");
check_rights_by_id(35);
$can_action = array('show', 'update');

if (empty($_POST['action']) || !in_array($_POST['action'], $can_action)) {
    $_POST['action'] = 'show';
}

switch ($_POST['action']) {
    case 'show':
        if (intval($RT['v']['userId']) < 10000) {
            $_POST['action'] = 'error';
            eMsg(0, "用户ID非法!");
            break;
        }
        if ($ONEUSER = brk_select_one("users", "*", "WHERE user_id='" . intval($RT['v']['userId']) . "'")) {
            $_SESSION['BRK_editUserinfo'] = $ONEUSER;
        }
        break;
    case 'update':

        if (!key_exists('user_id', $_POST) || $_POST['user_id'] != $_SESSION['BRK_editUserinfo']['user_id'] || $_POST['user_id'] < 10000) {
            $_POST['action'] = 'error';
            eMsg(0, "用户ID非法! 您只可以打开一个编辑页面");
            break;
        }

        $dataarr = array();

        if ($_POST['user_login'] != $_SESSION['BRK_editUserinfo']['user_login']) {
            if (brk_select('users', "user_id", "WHERE user_login='" . $_POST['user_login'] . "' ")) {
                $_POST['action'] = 'error';
                eMsg(0, "不能将用户名修改为" . $_POST['user_login'] . '&nbsp;&nbsp;系统中已存在该用户');
                break;
            }
            if (strlen($_POST['user_login']) < 5) {
                $_POST['action'] = 'error';
                eMsg(0, "用户名太短");
                break;
            }
            $dataarr['user_login'] = $_POST['user_login'];
        }

        if ($_POST['user_pass'] != '') {
            if (strlen($_POST['user_pass']) < 4) {
                $_POST['action'] = 'error';
                eMsg(0, "密码太短");
                break;
            }
            $dataarr['user_pass'] = sha1($_POST['user_pass']);
        }

        if ($_POST['user_nicename'] != $_SESSION['BRK_editUserinfo']['user_nicename']) {
            if (strlen($_POST['user_nicename']) < 5) {
                $_POST['action'] = 'error';
                eMsg(0, "昵称太短");
                break;
            }
            $dataarr['user_nicename'] = $_POST['user_nicename'];
        }

        if ($_POST['user_email'] != $_SESSION['BRK_editUserinfo']['user_email']) {
            $dataarr['user_nicename'] = $_POST['user_nicename'];
        }
        if ($_POST['user_url'] != $_SESSION['BRK_editUserinfo']['user_url']) {
            $dataarr['user_url'] = $_POST['user_url'];
        }

        if (!empty($dataarr)) {
            if (brk_update('users', $dataarr, "WHERE user_id='" . intval($_SESSION['BRK_editUserinfo']['user_id']) . "'")) {
                eMsg(1, "更新用户成功");
            } else {
                eMsg(0, "更新用户失败");
            }
        }else{
            eMsg(0, "无更新");
        }

        

        break;
    default:
        break;
}
